/*******************************************************************************
 * Copyright (c) 2003 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui;

import net.sourceforge.perforce.core.resources.LocalFile;
import net.sourceforge.perforce.core.resources.LocalResource;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.dialogs.PropertyPage;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class P4FilePropertiesPage extends PropertyPage {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the file. */
  private IFile file;

  // ---------------------------------------------------------------------------
  // Protected Methods

  @Override
  protected Control createContents(Composite parent) {
    initialize();
    noDefaultAndApplyButton();

    Composite composite = new Composite(parent, SWT.NULL);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    GridLayout layout = new GridLayout();
    layout.numColumns = 4;
    composite.setLayout(layout);

    final LocalFile p4file = (LocalFile) LocalResource.createFromResource(file);
    if (!p4file.hasRemote()) {
      if (p4file.isIgnored()) {
        // TODO:
      } else {
        // TODO:
      }
    } else {
      ResourceSyncInfo syncInfo = p4file.getSyncInfo();

      // Depot Path
      createLabel(composite, "Depot Path:");
      createLabel(composite, syncInfo.getDepotFileName(), 3);

      // Client Path
      createLabel(composite, "Client Path:");
      createLabel(composite, syncInfo.getClientFileName(), 3);

      // File Type
      createLabel(composite, "File Type:");
      createLabel(composite, syncInfo.getHeadType());
      createLabel(composite, "", 2);

      // Head Revision
      createLabel(composite, "Head Revision:");
      createLabel(composite, Integer.toString(syncInfo.getHeadRev()));

      // Have Revision
      createLabel(composite, "Have Revision:");
      createLabel(composite, Integer.toString(syncInfo.getHaveRev()));

      // Head Action
      createLabel(composite, "Head Action:");
      createLabel(composite, syncInfo.getHeadAction());

      // Head Change
      createLabel(composite, "Head Change:");
      createLabel(composite, Integer.toString(syncInfo.getHeadChange()));
    }

    return composite;
  }

  //----------------------------------------------------------------------------
  // - Private Methods

  /**
   * Utility method that creates a label instance and sets the default layout
   * data.
   * 
   * @param parent the parent for the new label
   * @param text the text for the new label
   * @param span number of columns to span.
   * @return the new label
   */
  private Label createLabel(Composite parent, String text, int span) {
    Label label = new Label(parent, SWT.LEFT);
    label.setText(text);
    GridData data = new GridData();
    data.horizontalSpan = span;
    data.horizontalAlignment = GridData.FILL;
    label.setLayoutData(data);

    return label;
  }

  /**
   * Utility method that creates a label instance and sets the default layout
   * data, setting column span to 1.
   * 
   * @param parent the parent for the new label
   * @param text the text for the new label
   */
  private Label createLabel(Composite parent, String text) {
    return createLabel(parent, text, 1);
  }

  /** Initializes the page */
  private void initialize() {
    IAdaptable element = getElement();
    if (element instanceof IFile) {
      file = (IFile) element;
    } else {
      Object adapter = element.getAdapter(IFile.class);
      if (adapter instanceof IFile) {
        file = (IFile) adapter;
      }
    }
  }
}
